import React, { PropTypes, Component } from 'react'
import {browserHistory} from 'react-router'

import NavLink from './NavLink'
import SideNav from './SideNav'
import Header from './Header'
import {DynPath, getAdminInfo, ADMIN, REGION_ADMIN, SHOP_ADMIN} from './../publicAPI/publicAPI'

class App extends Component {

  constructor (props, context) {
      super(props, context)
      console.log("初始化")
      var _rWapperHeigh = document.body.scrollHeight;

      this.state = {
        navItem: [
         {itemName: "排班管理", route: DynPath('/'), onlyActiveOnIndex: true},
         {itemName: "理发师管理", route: DynPath("/BarberM")},
         {itemName: "门店管理", route: DynPath("/StoreM")},
         {itemName: "订单管理", route: DynPath("/OrderM")},
         {itemName: "服务补录", route: DynPath("/ServiceM")},
         {itemName: "首页横幅管理", route: DynPath("/BannerM")},
         {itemName: "客户信息管理", route: DynPath("/CustomerM")},
         {itemName: "统计报表", route: DynPath("/StatisticsM")},
         {itemName: "管理员账号管理", route: DynPath("/AdministratorM")},
         {itemName: "区域管理员管理", route: DynPath("/Areamanager")},
         {itemName: "全局参数管理", route: DynPath("/GlobalM")},
         {itemName: "积分管理", route: DynPath("/PointM")},
         {itemName: "钱包管理", route: DynPath("/WalletM")},
         {itemName: "服务管理", route: DynPath("/PrototypeServiceM")},
         {itemName: "优惠券管理", route: DynPath("/Coupon")},
         {itemName: "商城管理", route: DynPath("/MallIndex")},
         {itemName: "薪酬管理", route: DynPath("/SalaryM")},
         {itemName: "驾校报名管理", route: DynPath("/DrivingSchool")},
         {itemName: "提成管理", route: DynPath("/Commission")},
         {itemName: "提成记录", route: DynPath("/WithdrawRecord")},
         {itemName: "分销关系管理", route: DynPath("/Distribution")},
       ],
       rHeight: _rWapperHeigh,
       hasBeenMounted:false} // 初始化状态

      this.adminNav = [1, 2, 5, 8, 9, 10, 14, 15, 16, 17, 18]
  }

    componentDidMount(){
        if(!this.state.hasBeenMounted){
            console.log(document.documentElement.offsetHeight - 55)
            this.setState({rHeight:document.body.scrollHeight,hasBeenMounted:true})
        }
        this.filterNavItem();
    }
  filterNavItem() {
      let adminInfo = JSON.parse(sessionStorage.getItem("adminInfo"));
      if (!adminInfo) {
          getAdminInfo(() => {
              this.filterNavItem();
          });
          return ;
      }
      console.log(adminInfo)
      switch (adminInfo.clazz) {
        case ADMIN:
          if (adminInfo.dname === SHOP_ADMIN) {
              this.filterShopNav();

          }
          return;
        case REGION_ADMIN:
          this.filterRegionNav();
          break;
        default:
          this.filterRegionNav();
      }
  }

  //区域管理员过滤
  filterRegionNav() {
      let nav = this.state.navItem.filter( (item, index) => {
           if (this.adminNav.indexOf(index) === -1) return true;
           else return false;
      })
      this.setState({
          navItem: nav
      })
  }

  //商城过滤
  filterShopNav() {
      this.setState({
          navItem: [{itemName: "商城管理", route: DynPath("/MallIndex"), onlyActiveOnIndex: true}]
      })
      browserHistory.push('/MallIndex')
  }

  render() {
    return (
      <div>
        <Header />
        <SideNav navItem={this.state.navItem} jumpClick={this.handleJump} selfStyle={{height: this.state.rHeight}}/>
        {this.props.children}
      </div>
    )
  }
}

export default App
